(function(global,factory){
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    (global = global || self, global.storage = factory());
}(this,function(){

    let cloneType = ['assign','deepClone']

    , storage = {
        storageName:'storage'
        ,type:'localStorage'
        ,clone:{
            isEnable:'auto'
            ,cloneType:'assign'
        }
    }

    storage.extend = function(target,...settings){
        //数据类型匹配器
        let matcher = {
            base(key,value){
                this[key] = value
            }
            ,object(key,value){
                this[key] ? extendImpl(this[key],[value]) : this[key] = value
            }
            ,getType(value){
                //注意
                //typeof(['ck']) --> 'object'
                return typeof(value) == 'object'? 'object':'base';
            }
        }
        , extendImpl = function(target,settings){
            settings.forEach(setting=>{
                let keys = Object.keys(setting);
                keys.forEach(key=>{
                    let value = setting[key]
                    , type = matcher.getType(value)
                    matcher[type].call(target,key,value);
                })
            })
            return target;
        }
        return extendImpl(target,settings);
    }
    
    storage.getStorage = function(){
        return JSON.parse(window[storage.type].getItem(storage.storageName) || '{}');
    }

    storage.setItem = function(moduleId,key,value){
        let data;    
        if(value != undefined){
            data = this.getItem(moduleId) || {}
            //value是数组或对象 && 开启拷贝模式
            if(typeof value === 'object' && (this.clone.isEnable === true || this.clone.isEnable == 'auto')){
                if(this.clone.cloneType == 'deep'){
                    value = storage.extend(data[key]||{},value)
                }else{
                    value = Object.assign(data[key]||{},value)
                }
            }
            data[key] = value;
            this.setItem(moduleId,data);
        }else{
            if(key){
                data = storage.getStorage();
                data[moduleId] = key;
                window[storage.type].setItem(storage.storageName,JSON.stringify(data));
            }else{
                window[storage.type].setItem(storage.storageName,JSON.stringify(moduleId));
            }
        }
    }

    storage.getItem = function(moduleId,key){
        let data = storage.getStorage()
        ,module = data[moduleId];
        return key ? module ? module[key] : module : module
    }

    storage.removeItem = function(moduleId,key){
        let data = storage.getStorage()
        ,module = data[moduleId]
        key ? module ? delete module[key] : null : delete module
        window[storage.type].setItem(this.storageName,JSON.stringify(data))
    }

    storage.clear = function(){
        window[storage.type].removeItem(this.storageName);
    }

    //是否存在某个key
    storage.isExitKey = function(moudleId,key){
        let data = this.getItem(moudleId,key)
        return data ? true : false;
    }

    return storage;
}));